{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e06e7b36-d1b0-4367-8ff0-68ff640649a6",
   "metadata": {},
   "source": [
    "# Sparse Vision Transformers with xformers\n",
    "\n",
    "In this notebook, we will illustrate how one can use the functionality available in xformers to\n",
    "quickly adapt an already existing Vision Transformer to use xformers efficient sparse kernels.\n",
    "\n",
    "In this notebook, you'll see:\n",
    "- how to get **memory and speed improvements** on a standard ViT model by using xformers with 80% sparsity\n",
    "- how to scale up a standard ViT model so that it can handle sequence **lengths larger than 10k**\n",
    "\n",
    "Let's start with a few imports. In this notebook, we will be using the `VisionTransformer` from timm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0ea655b8-5e01-41c1-b064-37525fe3c931",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import copy\n",
    "import torch\n",
    "from torch import nn\n",
    "from torch.utils import benchmark\n",
    "\n",
    "import xformers.components.attention.attention_patterns as AP\n",
    "from xformers.components.attention.core import scaled_dot_product_attention\n",
    "from xformers.components.attention._sputnik_sparse import SparseCS\n",
    "\n",
    "import timm\n",
    "from timm.models.vision_transformer import VisionTransformer\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec7139ee-c701-423a-910b-51b7c16e4b4f",
   "metadata": {},
   "source": [
    "To facilitate benchmarking and memory profiling, let's define a function that takes a generic callable and executes it, measuring the execution time and the GPU memory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fef5fd04-5e83-4fb0-af21-379ab6d1f1fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def profile_model(fn, min_run_time=2):\n",
    "    torch.cuda.reset_peak_memory_stats()\n",
    "    torch.cuda.synchronize()\n",
    "    res = benchmark.Timer(\n",
    "        stmt='fn()',\n",
    "        globals={\"fn\": fn},\n",
    "        label=\"profile\",\n",
    "        sub_label=\"\",\n",
    "        description=\"\"\n",
    "    ).blocked_autorange(min_run_time=min_run_time)\n",
    "    torch.cuda.synchronize()\n",
    "    memory = torch.cuda.max_memory_allocated() / 2 ** 20\n",
    "    memory = f\"Memory used: {memory} MB\"\n",
    "    print(res)\n",
    "    print(memory)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b384c152-0e62-41cb-9c1e-611e8e5115fa",
   "metadata": {},
   "source": [
    "Now it comes the core of it. We will implement an `Attention` module following the same API and modules as timm's, but using our `scaled_dot_product_attention` function.\n",
    "It is worth noting that the `scaled_dot_product_attention` from xformers supports both dense and sparse tensors with the same API, so it can be a drop-in replacement\n",
    "for experimenting with both sparse and dense attention.\n",
    "\n",
    "Internally, `scaled_dot_product_attention` will dispatch to the sparse variants if the `attn_mask` is sparse, otherwise it will dispatch to the dense implementation.\n",
    "\n",
    "We provide support for both COO-sparse matrices (via PyTorch `sparse_coo_tensor`) and CSR matrices (via our custom implementation).\n",
    "The CSR implementation is significantly faster and should be preferred at pretty-much all times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1aaa3de6-4be6-4a5c-b8db-6cb6bd712984",
   "metadata": {},
   "outputs": [],
   "source": [
    "# almost drop-in replacement for timm attention\n",
    "# but using the sparsity-aware scaled_dot_product_attention from xformers\n",
    "class Attention(torch.nn.Module):\n",
    "    def __init__(\n",
    "        self,\n",
    "        dim,\n",
    "        num_heads=8,\n",
    "        qkv_bias=False,\n",
    "        attn_drop=0.0,\n",
    "        proj_drop=0.0,\n",
    "        attn_mask=None,\n",
    "    ):\n",
    "        super().__init__()\n",
    "        self.num_heads = num_heads\n",
    "\n",
    "        self.qkv = torch.nn.Linear(dim, dim * 3, bias=qkv_bias)\n",
    "        self.attn_drop = torch.nn.Dropout(attn_drop)\n",
    "        self.proj = torch.nn.Linear(dim, dim)\n",
    "        self.proj_drop = torch.nn.Dropout(proj_drop)\n",
    "        self.attn_mask = attn_mask\n",
    "\n",
    "    def forward(self, x):\n",
    "        B, N, C = x.shape\n",
    "        qkv = (\n",
    "            self.qkv(x)\n",
    "            .reshape(B, N, 3, self.num_heads, C // self.num_heads)\n",
    "            .permute(2, 0, 3, 1, 4)\n",
    "        )\n",
    "\n",
    "        qkv = qkv.flatten(1, 2)\n",
    "\n",
    "        q, k, v = qkv.unbind()\n",
    "\n",
    "        x = scaled_dot_product_attention(q, k, v, self.attn_mask, dropout=self.attn_drop)\n",
    "        x = x.reshape(B, self.num_heads, N, C // self.num_heads)\n",
    "\n",
    "        x = x.transpose(1, 2).reshape(B, N, C)\n",
    "        x = self.proj(x)\n",
    "        x = self.proj_drop(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a853593-b3df-4526-871f-9967e1942394",
   "metadata": {},
   "source": [
    "Let's write a function that given a model, will replace all instances of `timm.models.vision_transformer.Attention` with our own implementation, which leverages `scaled_dot_product_attention` from xformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fccb2a4a-b864-452c-8649-7138855709af",
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace_attn_with_xformers_one(module, att_mask):\n",
    "    module_output = module\n",
    "    if isinstance(module, timm.models.vision_transformer.Attention):\n",
    "        qkv = module.qkv\n",
    "        dim = qkv.weight.shape[1] * module.num_heads\n",
    "        module_output = Attention(dim, module.num_heads, attn_mask=att_mask)\n",
    "    for name, child in module.named_children():\n",
    "        module_output.add_module(name, replace_attn_with_xformers_one(child, att_mask))\n",
    "    del module\n",
    "    return module_output"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ece835a-a715-4f44-94d4-0fdeb1bf1386",
   "metadata": {},
   "source": [
    "Now it's time to create our Vision Transformer. Nothing unusual here. Note that we will be keeping a copy of the model, which will be the model to use sparse self-attention"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "16934f00-841b-4bc0-a140-8c8ab7aada34",
   "metadata": {},
   "outputs": [],
   "source": [
    "img_size = 224\n",
    "patch_size = 16\n",
    "\n",
    "model = VisionTransformer(img_size=img_size, patch_size=patch_size,\n",
    "                              embed_dim=96, depth=8, num_heads=8, mlp_ratio=3.,\n",
    "                              qkv_bias=False, norm_layer=nn.LayerNorm).cuda()\n",
    "\n",
    "model_sparse = copy.deepcopy(model)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0620e39a-1db5-407f-ad5d-8ddafcf9a8b9",
   "metadata": {},
   "source": [
    "What comes next is about creating a sparsity pattern that we will be using for our model.\n",
    "\n",
    "You can refer to the `2d_attention` tutorial for further information on how to build custom attention patterns with xformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ffe7f787-3398-47ba-86e6-b8d333713740",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequence length: 14x14 = 196\n",
      "Sparsity: 0.8061789721250534, nnz=1152\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/Z1A+gAAAACXBIWXMAAAsTAAALEwEAmpwYAAALfElEQVR4nO3dX6xlBXXH8e+vM8A4+AdoGyIMKTxQDCEIZkJRG9s4NIxIGB/6AJEGqsm8tBWNiYHwYPrWRGM0qdEQREkl8IBYCVFhihrTpFIHmFJgEChaGBgcWlIxEBkmrj6cQzLczIC5e599z7C+n+Tmnr3Pvnete5kf+885+65UFZLe/H5vrRuQNA3DLjVh2KUmDLvUhGGXmlg/ZbGjc0xt4NgpSy6FPz77pUFf/+gDG4/I2preb3iR/fVyDvVcpnzp7e05of4kWyartyzufGbXoK+/8KRzjsjamt49dTcv1POHDLuH8VIThl1qwrBLTQwKe5KtSX6W5PEkV4/VlKTxrTrsSdYBXwY+BJwJXJbkzLEakzSuIXv284DHq+qJqtoP3AJsG6ctSWMbEvaTgacOWt4zX/caSbYn2Zlk5yu8PKCcpCEWfoGuqq6rqs1Vtfkojll0OUmH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f3MP6iqSfZwVfUs8FSSM+artgAPT1Gb2eH7+Uk2zn//W1ibC5S3A1fMH18BfGeqwkm2Mjt9u6SqXpqqLlW15h/ARcyuSv4XcO2Edf+U2eHbA8Cu+cdFa/Dz/zlwx8Q1zwF2zn/2fwaOn7D23wOPAA8C/wQcs+B6NzO7PvAKs6OajwO/z+wq/GPAvwAnTFj7cWbXqV79N/fVKX7vvl1WamIZDuMlTcCwS00YdqkJwy41YdilJgy71IRhl5r4f997Y5TiItSwAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "H, W = img_size // patch_size, img_size // patch_size\n",
    "print(f\"Sequence length: {H}x{W} = {H * W}\")\n",
    "\n",
    "axial_pattern = AP.axial_2d_pattern(H, W)\n",
    "loc_2d_dist = AP.local_2d_pattern(H, W, distance=2, p=2.0)\n",
    "rand_pattern = torch.rand((H * W) ** 2).reshape(H * W, H * W) > 0.99\n",
    "\n",
    "gaus_2d_dist = AP.local_2d_gausian_distribution(H, W, sigma=5)\n",
    "sparsity = 0.97\n",
    "num_non_zeros = int((H * W) ** 2 * (1 - sparsity))\n",
    "random_gaus_2d_pattern = AP.random_pattern_from_probability_matrix(gaus_2d_dist, num_non_zeros)\n",
    "\n",
    "\n",
    "t_mask = axial_pattern | loc_2d_dist | rand_pattern | random_gaus_2d_pattern\n",
    "\n",
    "# and let's not forget to add a global attention for the cls_token\n",
    "mask = torch.ones((H * W + 1, H * W + 1), dtype=torch.bool)\n",
    "mask[1:, 1:] = t_mask\n",
    "\n",
    "print(f\"Sparsity: {1 - mask.float().mean().item()}, nnz={num_non_zeros}\")\n",
    "\n",
    "plt.imshow(mask[H * W // 2 + W // 2][1:].reshape(H, W))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "885350c5-3f16-49cd-89c6-55c53fb3d77e",
   "metadata": {},
   "source": [
    "From the print just above, we can see that the full `attn_mask` has a sparsity level of around 80%.\n",
    "\n",
    "Once we are happy with the sparsity pattern, the next step is to create the sparse matrix that holds our sparsity pattern.\n",
    "\n",
    "Everything is handled by `SparseCS` class.\n",
    "\n",
    "**Note: the optimized kernels require that the total number of nonzero elements in the full `attn_mask` to be a multiple of 4. This is handled by default in the implementation (by removing elements until the number of non zeros is a multiple of 4)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ef202f12-7622-42d3-bbf6-01fa964c5b21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8061789791027855\n"
     ]
    }
   ],
   "source": [
    "mask = SparseCS(mask, torch.device(\"cuda\"))\n",
    "print(1 - mask.values.shape[1] / (mask.shape[0] * mask.shape[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce894e15-97a1-4490-98a9-4c4d039e4cc3",
   "metadata": {},
   "source": [
    "Now we are ready to replace the dense attentions with with sparse versions which leverage the sparsity pattern that we have just created"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "232d065e-afc5-4d7e-88ee-7e7fad875c07",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_sparse = replace_attn_with_xformers_one(model_sparse, mask)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a809363-a1c4-4bda-ad0a-610c078d7e95",
   "metadata": {},
   "source": [
    "Let's new create an input tensor and benchmark both the sparse and the dense versions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "62c6d031-f4a1-4b89-a388-3b8f2f3c17a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "i = torch.rand(64, 3, img_size, img_size).cuda()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b420967-81ae-4c2a-ae20-7690c575c392",
   "metadata": {},
   "source": [
    "## Profiling the default (dense) model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f3f150fe-5b2c-49b2-92a6-285a6f67205e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Forward only\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbdf375a400>\n",
      "profile\n",
      "  Median: 22.70 ms\n",
      "  IQR:    0.09 ms (22.68 to 22.77)\n",
      "  88 measurements, 1 runs per measurement, 1 thread\n",
      "Memory used: 1790.81689453125 MB\n",
      "\n",
      "Forward + backward\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbdf0464760>\n",
      "profile\n",
      "  Median: 77.05 ms\n",
      "  3 measurements, 10 runs per measurement, 1 thread\n",
      "Memory used: 1975.78173828125 MB\n"
     ]
    }
   ],
   "source": [
    "print(\"Forward only\")\n",
    "profile_model(lambda : model(i))\n",
    "print(\"\")\n",
    "print(\"Forward + backward\")\n",
    "profile_model(lambda : model(i).sum().backward())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1959b60b-aa09-4823-be4a-32ff24910d28",
   "metadata": {},
   "source": [
    "## Profiling the sparsity-aware model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fab86e1c-fff6-4457-aaee-9ad8303ee2df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Forward only\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbdf0406460>\n",
      "profile\n",
      "  Median: 16.38 ms\n",
      "  IQR:    0.05 ms (16.37 to 16.42)\n",
      "  13 measurements, 10 runs per measurement, 1 thread\n",
      "Memory used: 822.93701171875 MB\n",
      "\n",
      "Forward + backward\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbdf375a040>\n",
      "profile\n",
      "  Median: 53.41 ms\n",
      "  IQR:    0.01 ms (53.40 to 53.41)\n",
      "  4 measurements, 10 runs per measurement, 1 thread\n",
      "Memory used: 835.51025390625 MB\n"
     ]
    }
   ],
   "source": [
    "print(\"Forward only\")\n",
    "profile_model(lambda : model_sparse(i))\n",
    "print(\"\")\n",
    "print(\"Forward + backward\")\n",
    "profile_model(lambda : model_sparse(i).sum().backward())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65f51323-0ab6-4af2-b731-dad87a65789d",
   "metadata": {},
   "source": [
    "This shows that not only the sparse model is ~30% faster, but it also uses less than half the memory compared to the dense counterpart. Great!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9654e78-daa2-4023-848e-4c4ae8b6e500",
   "metadata": {},
   "source": [
    "# Scaling ViT to sequence lengths of size larger than 10k\n",
    "\n",
    "Getting speed-ups for training / inference on standard workloads thanks to sparsity is nice, but by leveraging the memory savings from sparse computations we can actually easily scale up ViT to sequence lengths which are much larger than what was originally possible.\n",
    "\n",
    "Let's get back to the original model, but this time scale it up so that the sequence length is `112 x 112`, and use a 99.5% sparsity pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a6cc8a1e-18f1-423c-af1e-2f7e88d3b332",
   "metadata": {},
   "outputs": [],
   "source": [
    "img_size = 224\n",
    "patch_size = 2\n",
    "\n",
    "model_sparse = VisionTransformer(img_size=img_size, patch_size=patch_size,\n",
    "                              embed_dim=96, depth=8, num_heads=8, mlp_ratio=3.,\n",
    "                              qkv_bias=False, norm_layer=nn.LayerNorm).cuda()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1922754f-9a0b-42b3-9819-4a729c8611b0",
   "metadata": {},
   "source": [
    "Everything was the same as before, with the difference that the patch size is now `2x2`, which means that the sequence length is `112x112`, which is much larger than what has been used so far.\n",
    "\n",
    "Let's create a sparsity pattern to have roughly 99.5% sparsity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a0ff06e2-9485-4e80-b18c-88b1531d904c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequence length: 112x112 = 12544\n",
      "Sparsity: 0.9938480411656201, nnz=786759\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA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fL7FeDeRZFNavgdv5fGnEbGC/McYTeG/VvRsN1AFPBx5VnhSRZKLgvhljqoAHgV3AHuAAsJLouG+f6ek+DcjvRjQkgagjIinAK8DNxpjGrp8Zf0oe8HZVETkfqDXGrBzoa/eBA5gBPGaMmY5/HMhhRX8L71smcCH+RDUMSObo4njUsOI+RUMSqAJGdHlfENhmCRFx4k8AzxljFgc214hIfuDzfKDWgtBOAb4qImXAi/gfCR4BMkTks6GOVt27SqDSGLM88H4R/qQQDffty8BOY0ydMaYTWIz/XkbDfftMT/dpQH43oiEJrACKA7W1CfgrbZZYEYiICPAUsMkY83CXj5YAcwKv5+CvKxhQxpg7jTEFxphR+O/RX4wxlwMfAF+zOLZqoEJExgc2nQlsJAruG/7HgJkikhT4+X4Wm+X3rYue7tMS4IpAK8FM4ECXx4bwGeiKmh4qSs4DtgLbgf+yMI5T8RfF1gKrA1/n4X/2XgpsA94Hsiy+X7OBNwOvxwD/AkqBlwGXRTFNAz4J3LvXgMxouW/AfcBmYD3wR8Bl1X0DXsBfN9GJvwR1VU/3CX/F7+8Cvxfr8LdwhD0m7TasVJyLhscBpZSFNAkoFec0CSgV5zQJKBXnNAkoFec0CSgV5zQJKBXn/h/K3j5w6lIVDwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "H, W = img_size // patch_size, img_size // patch_size\n",
    "print(f\"Sequence length: {H}x{W} = {H * W}\")\n",
    "\n",
    "rand_pattern = torch.rand((H * W) ** 2).reshape(H * W, H * W) > 0.999\n",
    "\n",
    "gaus_2d_dist = AP.local_2d_gausian_distribution(H, W, sigma=16)\n",
    "sparsity = 0.995\n",
    "num_non_zeros = int((H * W) ** 2 * (1 - sparsity))\n",
    "random_gaus_2d_pattern = AP.random_pattern_from_probability_matrix(gaus_2d_dist, num_non_zeros)\n",
    "\n",
    "t_mask = rand_pattern | random_gaus_2d_pattern\n",
    "\n",
    "# and let's not forget to add a global attention for the cls_token\n",
    "mask = torch.ones((H * W + 1, H * W + 1), dtype=torch.bool)\n",
    "mask[1:, 1:] = t_mask\n",
    "\n",
    "print(f\"Sparsity: {1 - mask.float().mean().item()}, nnz={num_non_zeros}\")\n",
    "\n",
    "plt.imshow(mask[H * W // 2 + W // 2][1:].reshape(H, W))\n",
    "plt.show()\n",
    "\n",
    "mask = SparseCS(mask, torch.device(\"cuda\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddf7b6e9-95c3-465e-9a56-c813c30dd943",
   "metadata": {},
   "source": [
    "And now, as before, replace the dense attentions with the sparse ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6cd67fee-1736-4d6b-b63b-0ef087be9fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_sparse = replace_attn_with_xformers_one(model_sparse, mask)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67357795-a78e-43fa-a4f9-3a856d993370",
   "metadata": {},
   "source": [
    "Now create a random input and let's feed it to our model and benchmark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6ea14f36-2384-428e-888e-26c7004748a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "i = torch.rand(8, 3, img_size, img_size).cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d9bf472b-227f-441a-864d-8baa9658cda2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Forward only\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbea8033d00>\n",
      "profile\n",
      "  Median: 194.43 ms\n",
      "  IQR:    0.94 ms (193.73 to 194.67)\n",
      "  11 measurements, 1 runs per measurement, 1 thread\n",
      "Memory used: 8022.40283203125 MB\n",
      "\n",
      "Forward + backward\n",
      "<torch.utils.benchmark.utils.common.Measurement object at 0x7fbdf04af4f0>\n",
      "profile\n",
      "  Median: 633.81 ms\n",
      "  IQR:    3.66 ms (632.41 to 636.07)\n",
      "  4 measurements, 1 runs per measurement, 1 thread\n",
      "Memory used: 8207.640625 MB\n"
     ]
    }
   ],
   "source": [
    "print(\"Forward only\")\n",
    "profile_model(lambda : model_sparse(i))\n",
    "print(\"\")\n",
    "print(\"Forward + backward\")\n",
    "profile_model(lambda : model_sparse(i).sum().backward())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3703edc-d2c6-4ebd-946c-219cd8a17153",
   "metadata": {},
   "source": [
    "By using a very sparse matrix for the self-attention computation, we were able to run ViT with sequence length of 10k by using ~8GB of GPU memory for a batch size of 8.\n",
    "The speed / memory can be easily tuned by controlling the degree of sparsity of your `attn_mask`.\n",
    "\n",
    "# Wrapping up\n",
    "\n",
    "In this notebook, we've show one way of quickly getting started with using xformers in your codebase.\n",
    "We showed how to get both memory and speed savings by leveraging sparse computations, and we also showed that it is possible to runa full forward+backward on a ViT model whose sequence length is greater than 10k.\n",
    "\n",
    "We hope that xformers can enable new research directions on very large sequences.\n",
    "\n",
    "If you have questions, we will be happy to help you getting started with xformers!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
